using B.S.NewMedical.Api.Read.Application.Command.ContentManagement.VisitRecord;
using B.S.NewMedical.Api.Read.DTOS.VisitRecord;
using B.S.NewMedical.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace B.S.NewMedical.Api.Read.Controllers.ContentManagement.VisitRecord
{
    /// <summary>
    /// 就诊记录查询控制器
    /// </summary>
    [Route("api/ContentManagement/[controller]/[action]")]
    [ApiController]
    public class VisitRecordController : ControllerBase
    {
        private readonly IMediator _mediator;

        public VisitRecordController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 根据患者ID获取就诊记录列表（支持分页、时间区间查询、就诊地点模糊查询）
        /// </summary>
        /// <param name="patientId">患者ID</param>
        /// <param name="queryDto">查询条件</param>
        /// <returns>就诊记录分页列表</returns>
        /// <remarks>
        /// 示例：GET /api/ContentManagement/VisitRecord/patient/1?pageIndex=1&amp;pageSize=10&amp;visitTypeId=1&amp;visitTimeStart=2024-01-01&amp;visitTimeEnd=2024-12-31&amp;visitLocation=门诊
        /// 根据患者ID查询该患者的所有就诊记录，支持各种筛选条件
        /// </remarks>
        [HttpGet]
        public async Task<ApiPageing<List<VisitRecordDto>>> GetVisitRecordsByPatientId(int patientId, [FromQuery] VisitRecordQueryDto queryDto)
        {
            if (patientId <= 0)
            {
                return new ApiPageing<List<VisitRecordDto>>
                {
                    Code = ApiEnum.Fail,
                    Msg = "患者ID必须大于0",
                    Data = new List<VisitRecordDto>()
                };
            }

            // 确保查询条件中的PatientId与路径参数一致
            if (queryDto == null)
            {
                queryDto = new VisitRecordQueryDto();
            }
            queryDto.PatientId = patientId;

            return await _mediator.Send(new GetVisitRecordsByPatientIdCommand(patientId, queryDto));
        }

        /// <summary>
        /// 根据患者ID获取最近的就诊记录（用于快速查看）
        /// </summary>
        /// <param name="patientId">患者ID</param>
        /// <param name="limit">返回记录数量，默认10条</param>
        /// <returns>最近的就诊记录列表</returns>
        /// <remarks>
        /// 示例：GET /api/ContentManagement/VisitRecord/patient/1/recent?limit=5
        /// 获取患者最近的几次就诊记录，按时间倒序排列
        /// </remarks>
        [HttpGet]
        public async Task<ApiPageing<List<VisitRecordDto>>> GetRecentVisitRecords(int patientId, [FromQuery] int limit = 10)
        {
            if (patientId <= 0)
            {
                return new ApiPageing<List<VisitRecordDto>>
                {
                    Code = ApiEnum.Fail,
                    Msg = "患者ID必须大于0",
                    Data = new List<VisitRecordDto>()
                };
            }

            if (limit <= 0 || limit > 50)
            {
                limit = 10; // 默认10条，最多50条
            }

            var queryDto = new VisitRecordQueryDto
            {
                PatientId = patientId,
                PageIndex = 1,
                PageSize = limit
            };

            return await _mediator.Send(new GetVisitRecordsByPatientIdCommand(patientId, queryDto));
        }
    }
}